/////////////////////////////////////////////////////////////////////////////
// Copyright(c) 2012-2015 purang All Rights Reserved
// Name:        thread_win.h
// Purpose:
// Author:      syy
// Modified by:
// Created:     2014/1/3 14:27
// RCS-ID:
// Licence:
/////////////////////////////////////////////////////////////////////////////
#ifndef PR_SYSTEM_WRAPPERS_SOURCE_THREAD_WIN_H_
#define PR_SYSTEM_WRAPPERS_SOURCE_THREAD_WIN_H_

#include <windows.h>

#include "critical_section_wrapper.h"
#include "event_wrapper.h"
#include "thread_wrapper.h"

namespace qb {

class ThreadWindows : public ThreadWrapper {
 public:
  ThreadWindows(ThreadRunFunction func, ThreadObj obj, ThreadPriority prio,
                const char* thread_name);
  virtual ~ThreadWindows();

  virtual bool Start(unsigned int& id);
  bool SetAffinity(const int* processor_numbers,
                   const unsigned int amount_of_processors);
  virtual bool Stop();
  virtual void SetNotAlive();
  virtual bool Stop(bool& force);
  virtual void SetThreadAfterFunc(ThreadAfterFunction afterFunc);

  static unsigned int WINAPI StartThread(LPVOID lp_parameter);

 protected:
  virtual void Run();

 private:
  ThreadRunFunction run_function_;
  ThreadAfterFunction after_function;
  ThreadObj obj_;

  bool alive_;
  bool dead_;

  // TODO(hellner)
  // do_not_close_handle_ member seem pretty redundant. Should be able to remove
  // it. Basically it should be fine to reclaim the handle when calling stop
  // and in the destructor.
  bool do_not_close_handle_;
  ThreadPriority prio_;
  EventWrapper* event_;
  CriticalSectionWrapper* critsect_stop_;
  CriticalSectionWrapper* critsect_interface_;

  HANDLE thread_;
  unsigned int id_;
  char name_[kThreadMaxNameLength];
  bool set_thread_name_;
};

}  // namespace qb

#endif  // PR_SYSTEM_WRAPPERS_SOURCE_THREAD_WIN_H_
